package cn.line5.seam.db;

import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.line5.seam.cache.DBConfigCache;
import cn.line5.seam.contexts.JdbcContext;
import cn.line5.seam.exception.DBException;

/**
 * <h1>执行器实现类</h1>
 * 
 * @author Liu,Xiaojun
 * @since JDK1.6
 */
public class ExecuteImpl extends AbstractQuery implements Query {

	private static final Logger log = LoggerFactory
			.getLogger(ExecuteImpl.class);

	/**
	 * 构造方法
	 * 
	 * @param queryString
	 *            SQL语句
	 */
	public ExecuteImpl(String queryString) {
		super(queryString);
	}

	/**
	 * 构造方法
	 * 
	 * @param queryString
	 *            SQL语句
	 * @param obj
	 *            条件对象，将根据SQL对应的:xxx调用条件对象对应的属性值
	 */
	public ExecuteImpl(String queryString, Object obj) {
		super(queryString, obj);
	}

	@Override
	/**
	 * 执行SQL
	 * @return 执行成功的条数
	 * @throws DBException	SQL执行异常
	 */
	public int execute() throws DBException {
		formatSql();
		int result = -1;
		if (DBConfigCache.showsql)
			log.info("LSEAM execute sql: " + sql);
		try {
			conn = JdbcContext.getConnection();
			ps = conn.prepareStatement(sql);
			setParameter();
			result = ps.executeUpdate();
		} catch (SQLException e) {
			throw new DBException("Error in execute, the SQL is:", sql, e);
		}
		return result;
	}
}